﻿namespace Singleton
{
    internal class LazySingleton
    {
        private static LazySingleton instance;
        private static object o = new();
        private LazySingleton()
        {
            Console.WriteLine("生成了一个实例");
        }

        public static LazySingleton GetInstance()
        {
            if (instance is null)
            {
                lock (o)
                {
                    if (instance is null)
                    {
                        instance = new LazySingleton();
                    }
                }

            }
            return instance;
        }

        // 也可使用这种方式
        private static readonly Lazy<LazySingleton> _instance = new(() => new LazySingleton());
    }
}
